const baseUrl = 'http://localhost:2233'

const http = {
    // 获取请求
    get: async (url, params = {}) => {
        const token = localStorage.getItem('auth_token');
        const queryString = new URLSearchParams(params).toString();
        const fullUrl = queryString ? `${baseUrl}${url}?${queryString}` : url;

        const response = await fetch(fullUrl, {
            method: 'GET',
            headers: {
                'Authorization': `Bearer ${token}`,
                'Content-Type': 'application/json',
            },
        });

        if (!response.ok) {
            throw new Error(`HTTP error! status: ${response.status}`);
        }

        return await response.json();
    },

    // 提交请求
    post: async (url, data = {}) => {
        const token = localStorage.getItem('auth_token');

        const response = await fetch(`${baseUrl}${url}`, {
            method: 'POST',
            headers: {
                'Authorization': `Bearer ${token}`,
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(data),
        });

        if (!response.ok) {
            throw new Error(`HTTP error! status: ${response.status}`);
        }

        return await response.json();
    }
}

export default http